﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

using com.ubosi.spider.core.Net;
using com.ubosi.spider.config;
using com.ubosi.common;
using System.IO;

namespace com.ubosi.spider.app
{
    public class SpiderGlobal
    {
      
        private IWebProxyContainer _proxyContainer;
        private WebRequestSetting _requestSettings;
        private SpiderConfig _SpiderConfig;


        private SpiderGlobal()
        {
            WebRequestSetting requestSettings = new WebRequestSetting();
            requestSettings.Accept = SystemConfigManager.SystemConfig.NetWorkConfig.Accept;
            requestSettings.UserAgent = SystemConfigManager.SystemConfig.NetWorkConfig.UserAgent;
            requestSettings.Timeout = SystemConfigManager.SystemConfig.NetWorkConfig.Timeout;
            requestSettings.Referer = SystemConfigManager.SystemConfig.NetWorkConfig.Referer;
            requestSettings.AutomaticDecompression = SystemConfigManager.SystemConfig.NetWorkConfig.DecompressionMethods;
            requestSettings.AllowAutoRedirect = SystemConfigManager.SystemConfig.NetWorkConfig.AllowAutoRedirect;
            requestSettings.TryCount = SystemConfigManager.SystemConfig.NetWorkConfig.TryCount;
            if (SystemConfigManager.SystemConfig.NetWorkConfig.Headers != null)
            {
                requestSettings.Headers = new List<KeyValuePair<HttpRequestHeader, string>>();
                foreach (Header header in SystemConfigManager.SystemConfig.NetWorkConfig.Headers)
                {
                    requestSettings.Headers.Add(new KeyValuePair<HttpRequestHeader, string>(header.Name, header.Value));
                }
            }
            _requestSettings = requestSettings;

            IWebProxyContainer proxyContainer = new SpiderWebProxyContainer();
            if (SystemConfigManager.SystemConfig.NetWorkConfig.UseProxy)
            {//使用代理服务器
                if (SystemConfigManager.SystemConfig.NetWorkConfig.Proxys != null &&
                    SystemConfigManager.SystemConfig.NetWorkConfig.Proxys.Count > 0)
                {
                    foreach (Proxy proxy in SystemConfigManager.SystemConfig.NetWorkConfig.Proxys)
                    {
                        WebProxy webProxy = new WebProxy(proxy.Host, proxy.Port);
                        if (!string.IsNullOrEmpty(proxy.User))
                        {
                            webProxy.Credentials = new NetworkCredential(proxy.User, proxy.Password, proxy.Domain);
                        }
                        proxyContainer.Add(webProxy);
                    }
                }
            }
            _proxyContainer = proxyContainer;

            _SpiderConfig = (SpiderConfig)SerializerUtility.DeSerializeFromFile(typeof(SpiderConfig), Path.Combine(Environment.CurrentDirectory + "\\Config\\", "SpiderConfig.xml"));
        }
        private static SpiderGlobal _theInstance = null;

        static SpiderGlobal()
        {
            _theInstance = new SpiderGlobal();
        }
        public static IWebProxyContainer ProxyContainer
        {
            get
            {
                return _theInstance._proxyContainer;
            }
        }
        public static WebRequestSetting RequestSetting
        {
            get
            {
                return _theInstance._requestSettings;
            }
        }
        public static SpiderConfig SpiderConfig
        {
            get
            {
                return _theInstance._SpiderConfig;
            }
        }
    }
}
